import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Test3 {
    public static void main(String[] args) throws IOException {
        //创建DatagramSocket对象
        DatagramSocket socket = new DatagramSocket(8080);
        //创建DatagramPacket对象
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
        for (int i = 0; i < 100; i++) {
            //接收数据
            socket.receive(packet);
            //打印接收到的数据
            String message = new String(packet.getData(), 0, packet.getLength());
            System.out.println("接收到的数据：" + message);
//            //打印发送方的地址和端口号
//            System.out.println("发送方的地址：" + packet.getAddress().getHostAddress());
//            System.out.println("发送方的端口号：" + packet.getPort());
        }
        //关闭DatagramSocket对象
        socket.close();
    }
}
